package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    /**
     * 删除菜品，同时需要删除菜品和菜品口味的关联数据
     * @param ids
     */
    @Autowired
    private DishFlavorService dishFlavorService;
    @Override
    @Transactional
    public void removeWithFlavor(List<Long> ids) {
//        select count(*) from dish where id in(1,2,3) and status=1;
//        查询菜品状态，确定是否可用删除
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = this.count(queryWrapper);
        if(count>0){
//            如果不能删除，就抛出一个异常
            throw new CustomException("菜品正在售卖中,不能删除");
        }
//        如果可以删除，先删除菜品中的数据
        this.removeByIds(ids);

//        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
//        queryWrapper1.in(Dish::getCategoryId,ids);
//       this.remove(queryWrapper1);

//        delete from dish_flaor where dish_id in(1,2,3)
        LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(DishFlavor::getDishId,ids);
        //        删除关系表中的数据
        dishFlavorService.remove(queryWrapper1);
    }


//菜品的起售和停售
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids!=null,Dish::getId,ids);
        List<Dish> list = this.list(queryWrapper);

        for (Dish dish : list) {
            dish.setStatus(status);
            this.updateById(dish);
        }

    }
//保存菜品的口味信息
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        //菜品id
        Long id = dishDto.getId();
        //保存菜品口味数据到菜品口味表dish_flavor
        List<DishFlavor> flavors = dishDto.getFlavors();

        //循环给每个口味设置菜品的id
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        dishFlavorService.saveBatch(flavors);

    }

    @Autowired
    private DishMapper dishMapper;
    @Override
    public Page<DishDto> pageByName(int page, int pageSize, String name) {
        Page<DishDto> pageInfo = new Page<>(page, pageSize);
         dishMapper.pageByName(pageInfo, name);
         return pageInfo;
    }
    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        Dish dish= this.getById(id);
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavorList = dishFlavorService.list(lqw);

//        封装到dto
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavorList);
        return dishDto;

    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        //delete from dish_flavor where dish_id = 21314
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //3. 添加新的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        //循环给每个口味设置菜品的id
        Long id = dishDto.getId();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        //批量保存
        dishFlavorService.saveBatch(flavors);
    }


}
